/*
 * Copyright (c) 2009 Roman Savelyev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>
#include <hlplch.h>

#include <AppHook.rsg>

#include "AppHook.hrh"
#include "AppHook.pan"
#include "AppHookApplication.h"
#include "AppHookAppUi.h"
#include "AppHookAppView.h"
#include "Log.h"

void CAppHookAppUi::ConstructL()
    {
    LOG(_L("-->CAppHookAppUi::ConstructL"));
    BaseConstructL(CAknAppUi::EAknEnableSkin);
    iAppView = CAppHookAppView::NewL(ClientRect());
    iModel = new(ELeave) CAppHookModel(*this);
    LOG(_L("<--CAppHookAppUi::ConstructL"));
    }

CAppHookAppUi::~CAppHookAppUi()
    {
    LOG(_L("-->CAppHookAppUi::~CAppHookAppUi"));
    delete iAppView;
    delete iModel;
    LOG(_L("<--CAppHookAppUi::~CAppHookAppUi"));
    }

void CAppHookAppUi::HandleCommandL(TInt aCommand)
    {
    LOG2(_L("-->CAppHookAppUi::HandleCommandL(%d)"), aCommand);
    switch (aCommand)
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            {
            Exit();
            }
            break;
        case ECommandConnect:
            {
            iModel->ConnectL();
            }
            break;
        case ECommandDisconnect:
            {
            iModel->Disconnect();
            }
            break;
        case ECommandSettings:
            {

            }
            break;
        case EAbout:
            {
            CAknMessageQueryDialog* dlg =
                    new (ELeave) CAknMessageQueryDialog();
            dlg->PrepareLC(R_ABOUT_QUERY_DIALOG);
            HBufC* title = iEikonEnv->AllocReadResourceLC(
                    R_ABOUT_DIALOG_TITLE);
            dlg->QueryHeading()->SetTextL(*title);
            CleanupStack::PopAndDestroy(); //title
            HBufC* msg = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TEXT);
            dlg->SetMessageTextL(*msg);
            CleanupStack::PopAndDestroy(); //msg
            dlg->RunLD();
            }
            break;
        default:
            LOG(_L("   CAppHookAppUi::HandleCommandL - UNKNOWN COMMAND"));
            Panic(EAppHookUi);
            break;
        }
    LOG(_L("<--CAppHookAppUi::HandleCommandL"));
    }

void CAppHookAppUi::HandleStatusPaneSizeChange()
    {
    LOG(_L("-->CAppHookAppUi::HandleStatusPaneSizeChange"));
    iAppView->SetRect(ClientRect());
    LOG(_L("<--CAppHookAppUi::HandleStatusPaneSizeChange"));
    }

void CAppHookAppUi::SandboxingComplete(MAppHookModelObserver::TSandboxingStage aStage, TInt aError)
    {
    LOG3(_L("-->CAppHookAppUi::SandboxingComplete(%d, %d)"), aStage, aError);
    LOG(_L("<--CAppHookAppUi::SandboxingComplete"));
    }
